#ifndef ATOMCONTROL_H
#define ATOMCONTROL_H

#include "HelmetOutputs.h"
#include "HelmetI2C.h"

/**************************************************************************************
	Global Variables
**************************************************************************************/
unsigned short int AtomState = 0;
unsigned short int AtomResponse = 0;
unsigned short int ProjOverride = 0;

/**************************************************************************************
	Function Prototypes
**************************************************************************************/
void AtomStartUp();
void AtomShutDown();

/**************************************************************************************
	Functions
**************************************************************************************/
void AtomStartUp() {
	unsigned int i=0;
	WriteAtom("Sleep Starting Up Atom\r\n");
	// Turn on Atom's Power
	Enable12V();
	// Wait for Atom to respond
	WriteAtom("Sleep Waiting for Atom Response\r\n");
	while( AtomResponse == 0 )
	{
		if( EnterSleepMode ) {
			// ProjOverride = 1; // do not turn toggle projector when shutdown occurs
			return;
		}
		i=12000000; // ~1 sec of waiting
		while( i>0 ) i--;
		VerifyFuelGaugeStatus();
		//OutputFuelGaugeData();
	}
	ProjOverride = 0; // be sure to toggle projector at shutdown
	// Assume Atom is now on
	AtomState = 1;
	// Powering up projector
	WriteAtom("Sleep Power projector on...");
	Enable5V();
	i=6000000; // ~1/2 sec of waiting for stability
	while( i>0 ) i--;
	WriteAtom("done\r\n");
	// power button projector
	WriteAtom("Sleep Starting Projector...");
	ProjectorOnOff();
	WriteAtom("done\r\n");
}

void AtomShutDown() {
	unsigned int i=0;
	WriteAtom ("Sleep ENTER\r\n");
	// Disable 5V rail (buttons, fans, projector)
	Disable5V();
	WriteAtom("Sleep Projector power down\r\n");
	//if( ProjOverride == 0 ) {
	//	WriteAtom("Sleep Shutting Down Projector...");
	//	ProjectorOnOff();
	//	WriteAtom("done\r\n");
	//}
	WriteAtom("Sleep Waiting while Atom shuts down\r\n");
	// Wait for Atom to process stuff
	// Also will let interrupts finish
	i=100000000; // ~8 secs of waiting
	while( i>0 ) i--;
	WriteAtom("Sleep Turning off Atom power\r\n");
	// Disable Atom Power Rail
	Disable12V();
	// Assume Atom is now off (and is not responding)
	AtomState = 0;
	AtomResponse = 0;
}	

#endif